package com.small.flowable.modules.app.controllers;

import com.common.basis.result.Result;
import com.common.basis.result.ResultSet;
import com.small.flowable.modules.app.services.TaskInstanceService;
import lombok.AllArgsConstructor;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ActivityInstance;
import org.flowable.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;


/**
 * 测试mybatis-plus 引入
 *
 * @author wangguochao
 */
@RequestMapping("app")
@RestController
@AllArgsConstructor
public class TestEntityController {

    @Autowired
    private TaskInstanceService taskInstanceService;
    @Autowired
    private TaskService taskService;
    /**
     * 运行时服务
     */
    @Autowired
    private RuntimeService runtimeService;
    /**
     * 仓库服务
     */
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    protected HistoryService historyService;

    @Autowired
    protected ManagementService managementService;

    /**
     * 测试新建
     *
     * @return
     */
    @RequestMapping(value = "processDiagram")
    public void genProcessDiagram(HttpServletResponse httpServletResponse, String processDefinitionId) throws Exception {

        //获取流程图
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        DefaultProcessDiagramGenerator diagramGenerator = new DefaultProcessDiagramGenerator();
        byte[] buf = new byte[1024];
        int length = 0;
        try (InputStream in = diagramGenerator
                .generateDiagram(
                        bpmnModel,
                        "bmp",
                        "宋体",
                        "宋体",
                        "宋体",
                        null,
                        1.0,
                        true);
             OutputStream out = httpServletResponse.getOutputStream()) {
            while ((length = in.read(buf)) != -1) {
                out.write(buf, 0, length);
            }
        }
    }


    /**
     * 测试新建
     *
     * @return
     */
    @RequestMapping(value = "test")
    public Result<BpmnModel> test(String processDefinitionId) throws Exception {
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId("f5ca8f69-59a9-11eb-a1f5-acde48001122")
                .list();
        List<ActivityInstance> list1 = runtimeService.createActivityInstanceQuery()
                .processInstanceId("f5ca8f69-59a9-11eb-a1f5-acde48001122").list();
        //获取流程图
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        return ResultSet.go(bpmnModel);
    }


}
